在產出安全簽章前必須完成以下任務,要先取得 Nonce、Hash ID、訊息內文,才可計算出安全簽章,
產生前請注意以下事項:
{
"ShopNo": "BA0026_001",
"OrderNo": "A201804270001",
"Amount": 50000,
"CurrencyID": "TWD",
"PayType": "A",
"ATMParam": { "ExpireDate": "20180502" },
"CardParam": { },
"ConvStoreParam": { },
"PrdtName": "虛擬帳號訂單",
"ReturnURL": "http://10.11.22.113:8803/QPay.ApiClient/Store/Return",
"BackendURL": "http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess"
}
使用python的json套件,安裝如下:
pip install import json
程式如下,值得注意的地方是json.dumps,在處理中文時,會轉換為ascii,要加入參數ensure_ascii=False,才會與範例一致。
send_message_ori = {
"ShopNo": "BA0026_001",
"OrderNo": "A201804270001",
"Amount": 50000,
"CurrencyID": "TWD",
"PayType": "A",
"ATMParam": {"ExpireDate": "20180502"},
"CardParam": {},
"ConvStoreParam": {},
"PrdtName": "虛擬帳號訂單",
"ReturnURL": "http://10.11.22.113:8803/QPay.ApiClient/Store/Return",
"BackendURL": "http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess",
}
send_message={k: v for k, v in send_message_ori.items() if v and type(v)!=dict }
send_message = {k: send_message[k] for k in sorted ( send_message.keys())}
type(json.dumps(send_message))
send_message_str=json.dumps(send_message, ensure_ascii=False)
send_message_ContentHash=send_message_str.replace('": "', "=").replace('": ', "=").replace('", "', "&").replace(', "', "&")[2:][:-2]
print(send_message_ContentHash)
結果
Amount=50000&BackendURL=http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess&CurrencyID=TWD&OrderNo=A201804270001&PayType=A&PrdtName=虛擬帳號訂單&ReturnURL=http://10.11.22.113:8803/QPay.ApiClient/Store/Return&ShopNo=BA0026_001
NonceValue='NjM2NjA0MzI4ODIyODguMzo3NzI0ZDg4ZmI5Nzc2YzQ1MTNhYzg2MTk3NDBlYTRhNGU0N2IxM2Q2M2JkMTIwOGU5YzZhMGFmNGY5MjA5YzVm'
send_message_TextHash=send_message_ContentHash+NonceValue+hashID
print(send_message_TextHash)
結果如下
Amount=50000&BackendURL=http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess&CurrencyID=TWD&OrderNo=A201804270001&PayType=A&PrdtName=虛擬帳號訂單&ReturnURL=http://10.11.22.113:8803/QPay.ApiClient/Store/Return&ShopNo=BA0026_001NjM2NjA0MzI4ODIyODguMzo3NzI0ZDg4ZmI5Nzc2YzQ1MTNhYzg2MTk3NDBlYTRhNGU0N2IxM2Q2M2JkMTIwOGU5YzZhMGFmNGY5MjA5YzVm17D8E6558DC60E702A6B57E1B9B7060D
pip install import hashlib
執行如下
s2 = hashlib.sha256()
s2.update(send_message_TextHash.encode('utf-8'))
SignValue = s2.hexdigest().upper()
print(SignValue)
這篇的重點-安全簽章就完成囉~
A3EAEE3B361B7E7E9B0F6422B954ECA5D54CEC6EAB0880CB484AA6FDA4154331
下一步就是 Message的產製囉
雖然這次鐵人賽在非我預期的情況下中斷了~但如第一篇說的,
頭洗下去就繼續吧!當作給自己的一個交待囉~